<?php

namespace app\portal\controller;

use addons\wechat\model\WechatCaptcha;
use app\common\controller\Frontend;
use app\common\library\Ems;
use app\common\library\Sms;
use app\common\model\Attachment;
use think\Config;
use think\Cookie;
use think\Hook;
use think\Session;
use think\Validate;

/**
 * 会员中心
 */
class Index extends Frontend
{
    protected $layout = 'default';
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $auth = $this->auth;

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

        //监听注册登录退出的事件
        Hook::add('user_login_successed', function ($user) use ($auth) {
            $expire = input('post.keeplogin') ? 30 * 86400 : 0;
            Cookie::set('uid', $user->id, $expire);
            Cookie::set('token', $auth->getToken(), $expire);
        });
        Hook::add('user_register_successed', function ($user) use ($auth) {
            Cookie::set('uid', $user->id);
            Cookie::set('token', $auth->getToken());
        });
        Hook::add('user_delete_successed', function ($user) use ($auth) {
            Cookie::delete('uid');
            Cookie::delete('token');
        });
        Hook::add('user_logout_successed', function ($user) use ($auth) {
            Cookie::delete('uid');
            Cookie::delete('token');
        });
        $this->model = new \app\admin\model\yexam\Subject;
    }
    /**
     * 注册会员
     */
    public function index()
    {
       $this->view->assign('title', __('牛课教育首页'));
	   return $this->view->fetch();
	   
    }
	
	    public function alipay()
    {
        $this->view->assign('title', __('牛课宝典首页'));
		return $this->view->fetch();
    }
	    public function pay()
    {
        $this->view->assign('title', __('牛课宝典【科目名】在线购买'));
		return $this->view->fetch();
    }
	
	
	//测试发码接口
	public function ksbao_test_send_cdk(){
		
		//$out_trade_no = $this->request->param('out_trade_no');
		$out_trade_no = '202105261913173645647';
		
		$xorder = \app\admin\model\Xorder::get(['order_no' => $out_trade_no]);
		//var_dump($xorder);exit;
		$package = \app\admin\model\Package::get($xorder['sale_product_id']);
		$vn = $package['platform_vn'] ? : '0';
		$ksbao = cmf_get_ksbao_sendcdk_param();
		$url = $ksbao['domain'] . $ksbao['sendcdk'];
		$param = [
			"agent_code"=>$ksbao['agent_code'],
			"secretKey"=>$ksbao['secretKey'],
			"trade_no"=>$xorder['order_no'],
			"username"=>$xorder['mobile'],
			"price"=>$xorder['total_amount']/100,
			"appename"=>$xorder['sale_en_name'],
			"vn"=>$vn,
		];
		var_dump($url);
		var_dump($param);
		$res = cmf_send_post($url,$param);
		$res_array = json_decode($res,true);
		var_dump($res_array);
		
		if($res_array['code'] == 0){
			$cdk = $res_array['data'];
			$mobile = '';
			if($xorder) $mobile = $xorder['mobile'];
			if(!cmf_check_mobile($mobile)){
				//发码成功  但是短信发送失败
			} 
			$config = get_addon_config('alisms');
			//var_dump($config);exit;
			$sign = $config['sign'];
			$template = $config['template']['sendCDK'];
			$param = ['code'=>$cdk];
			$alisms = new \addons\alisms\library\Alisms();
			$ret = $alisms->mobile($mobile)
				->template($template)
				->sign($sign)
				->param($param)
				->send();
				
			if ($ret) {
            // $this->success("发送成功,记得保存哦,否则CDK将不会有记录");
				//发码成功  短信发送成功
				echo "发码成功  短信发送成功";
			} else {
				//发码成功  但是短信发送失败
				echo "发码成功  短信发送成功";
			}
		}
		
	}
	
	//班次系列对照表
	public function get_class(){
		$url = cmf_get_ksbao_sendcdk_param('domain') . cmf_get_ksbao_sendcdk_param('getclass');
		$param['agent_code'] = cmf_get_ksbao_sendcdk_param('agent_code');
		$param['secretKey'] = cmf_get_ksbao_sendcdk_param('secretKey');
		
		$res = cmf_send_get($url,$param);
		$res_array = json_decode($res,true);
		var_dump($res_array);
	}
	
}
